namespace gpuip {

// compile with this header file included to enable overload math functions.
// please notice that this file was included in the CUDA samples
std::string get_cuda_helper_math() {
    return std::string("\n"
"/**\n"
" * Copyright 1993-2013 NVIDIA Corporation.  All rights reserved.\n"
" *\n"
" * Please refer to the NVIDIA end user license agreement (EULA) associated\n"
" * with this source code for terms and conditions that govern your use of\n"
" * this software. Any use, reproduction, disclosure, or distribution of\n"
" * this software and related documentation outside the terms of the EULA\n"
" * is strictly prohibited.\n"
" *\n"
" */\n"
"\n"
"/*\n"
" *  This file implements common mathematical operations on vector types\n"
" *  (float3, float4 etc.) since these are not provided as standard by CUDA.\n"
" *\n"
" *  The syntax is modeled on the Cg standard library.\n"
" *\n"
" *  This is part of the Helper library includes\n"
" *\n"
" *    Thanks to Linh Hah for additions and fixes.\n"
" */\n"
"\n"
"#ifndef HELPER_MATH_H\n"
"#define HELPER_MATH_H\n"
"\n"
"#include \"cuda_runtime.h\"\n"
"\n"
"typedef unsigned int uint;\n"
"typedef unsigned short ushort;\n"
"\n"
"#ifndef EXIT_WAIVED\n"
"#define EXIT_WAIVED 2\n"
"#endif\n"
"\n"
"#ifndef __CUDACC__\n"
"#include <math.h>\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// host implementations of CUDA functions\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline float fminf(float a, float b)\n"
"{\n"
"    return a < b ? a : b;\n"
"}\n"
"\n"
"inline float fmaxf(float a, float b)\n"
"{\n"
"    return a > b ? a : b;\n"
"}\n"
"\n"
"inline int max(int a, int b)\n"
"{\n"
"    return a > b ? a : b;\n"
"}\n"
"\n"
"inline int min(int a, int b)\n"
"{\n"
"    return a < b ? a : b;\n"
"}\n"
"\n"
"inline float rsqrtf(float x)\n"
"{\n"
"    return 1.0f / sqrtf(x);\n"
"}\n"
"#endif\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// constructors\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline __host__ __device__ float2 make_float2(float s)\n"
"{\n"
"    return make_float2(s, s);\n"
"}\n"
"inline __host__ __device__ float2 make_float2(float3 a)\n"
"{\n"
"    return make_float2(a.x, a.y);\n"
"}\n"
"inline __host__ __device__ float2 make_float2(int2 a)\n"
"{\n"
"    return make_float2(float(a.x), float(a.y));\n"
"}\n"
"inline __host__ __device__ float2 make_float2(uint2 a)\n"
"{\n"
"    return make_float2(float(a.x), float(a.y));\n"
"}\n"
"\n"
"inline __host__ __device__ int2 make_int2(int s)\n"
"{\n"
"    return make_int2(s, s);\n"
"}\n"
"inline __host__ __device__ int2 make_int2(int3 a)\n"
"{\n"
"    return make_int2(a.x, a.y);\n"
"}\n"
"inline __host__ __device__ int2 make_int2(uint2 a)\n"
"{\n"
"    return make_int2(int(a.x), int(a.y));\n"
"}\n"
"inline __host__ __device__ int2 make_int2(float2 a)\n"
"{\n"
"    return make_int2(int(a.x), int(a.y));\n"
"}\n"
"\n"
"inline __host__ __device__ uint2 make_uint2(uint s)\n"
"{\n"
"    return make_uint2(s, s);\n"
"}\n"
"inline __host__ __device__ uint2 make_uint2(uint3 a)\n"
"{\n"
"    return make_uint2(a.x, a.y);\n"
"}\n"
"inline __host__ __device__ uint2 make_uint2(int2 a)\n"
"{\n"
"    return make_uint2(uint(a.x), uint(a.y));\n"
"}\n"
"\n"
"inline __host__ __device__ float3 make_float3(float s)\n"
"{\n"
"    return make_float3(s, s, s);\n"
"}\n"
"inline __host__ __device__ float3 make_float3(float2 a)\n"
"{\n"
"    return make_float3(a.x, a.y, 0.0f);\n"
"}\n"
"inline __host__ __device__ float3 make_float3(float2 a, float s)\n"
"{\n"
"    return make_float3(a.x, a.y, s);\n"
"}\n"
"inline __host__ __device__ float3 make_float3(float4 a)\n"
"{\n"
"    return make_float3(a.x, a.y, a.z);\n"
"}\n"
"inline __host__ __device__ float3 make_float3(int3 a)\n"
"{\n"
"    return make_float3(float(a.x), float(a.y), float(a.z));\n"
"}\n"
"inline __host__ __device__ float3 make_float3(uint3 a)\n"
"{\n"
"    return make_float3(float(a.x), float(a.y), float(a.z));\n"
"}\n"
"\n"
"inline __host__ __device__ int3 make_int3(int s)\n"
"{\n"
"    return make_int3(s, s, s);\n"
"}\n"
"inline __host__ __device__ int3 make_int3(int2 a)\n"
"{\n"
"    return make_int3(a.x, a.y, 0);\n"
"}\n"
"inline __host__ __device__ int3 make_int3(int2 a, int s)\n"
"{\n"
"    return make_int3(a.x, a.y, s);\n"
"}\n"
"inline __host__ __device__ int3 make_int3(uint3 a)\n"
"{\n"
"    return make_int3(int(a.x), int(a.y), int(a.z));\n"
"}\n"
"inline __host__ __device__ int3 make_int3(float3 a)\n"
"{\n"
"    return make_int3(int(a.x), int(a.y), int(a.z));\n"
"}\n"
"\n"
"inline __host__ __device__ uint3 make_uint3(uint s)\n"
"{\n"
"    return make_uint3(s, s, s);\n"
"}\n"
"inline __host__ __device__ uint3 make_uint3(uint2 a)\n"
"{\n"
"    return make_uint3(a.x, a.y, 0);\n"
"}\n"
"inline __host__ __device__ uint3 make_uint3(uint2 a, uint s)\n"
"{\n"
"    return make_uint3(a.x, a.y, s);\n"
"}\n"
"inline __host__ __device__ uint3 make_uint3(uint4 a)\n"
"{\n"
"    return make_uint3(a.x, a.y, a.z);\n"
"}\n"
"inline __host__ __device__ uint3 make_uint3(int3 a)\n"
"{\n"
"    return make_uint3(uint(a.x), uint(a.y), uint(a.z));\n"
"}\n"
"\n"
"inline __host__ __device__ float4 make_float4(float s)\n"
"{\n"
"    return make_float4(s, s, s, s);\n"
"}\n"
"inline __host__ __device__ float4 make_float4(float3 a)\n"
"{\n"
"    return make_float4(a.x, a.y, a.z, 0.0f);\n"
"}\n"
"inline __host__ __device__ float4 make_float4(float3 a, float w)\n"
"{\n"
"    return make_float4(a.x, a.y, a.z, w);\n"
"}\n"
"inline __host__ __device__ float4 make_float4(int4 a)\n"
"{\n"
"    return make_float4(float(a.x), float(a.y), float(a.z), float(a.w));\n"
"}\n"
"inline __host__ __device__ float4 make_float4(uint4 a)\n"
"{\n"
"    return make_float4(float(a.x), float(a.y), float(a.z), float(a.w));\n"
"}\n"
"\n"
"inline __host__ __device__ int4 make_int4(int s)\n"
"{\n"
"    return make_int4(s, s, s, s);\n"
"}\n"
"inline __host__ __device__ int4 make_int4(int3 a)\n"
"{\n"
"    return make_int4(a.x, a.y, a.z, 0);\n"
"}\n"
"inline __host__ __device__ int4 make_int4(int3 a, int w)\n"
"{\n"
"    return make_int4(a.x, a.y, a.z, w);\n"
"}\n"
"inline __host__ __device__ int4 make_int4(uint4 a)\n"
"{\n"
"    return make_int4(int(a.x), int(a.y), int(a.z), int(a.w));\n"
"}\n"
"inline __host__ __device__ int4 make_int4(float4 a)\n"
"{\n"
"    return make_int4(int(a.x), int(a.y), int(a.z), int(a.w));\n"
"}\n"
"\n"
"\n"
"inline __host__ __device__ uint4 make_uint4(uint s)\n"
"{\n"
"    return make_uint4(s, s, s, s);\n"
"}\n"
"inline __host__ __device__ uint4 make_uint4(uint3 a)\n"
"{\n"
"    return make_uint4(a.x, a.y, a.z, 0);\n"
"}\n"
"inline __host__ __device__ uint4 make_uint4(uint3 a, uint w)\n"
"{\n"
"    return make_uint4(a.x, a.y, a.z, w);\n"
"}\n"
"inline __host__ __device__ uint4 make_uint4(int4 a)\n"
"{\n"
"    return make_uint4(uint(a.x), uint(a.y), uint(a.z), uint(a.w));\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// negate\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline __host__ __device__ float2 operator-(float2 &a)\n"
"{\n"
"    return make_float2(-a.x, -a.y);\n"
"}\n"
"inline __host__ __device__ int2 operator-(int2 &a)\n"
"{\n"
"    return make_int2(-a.x, -a.y);\n"
"}\n"
"inline __host__ __device__ float3 operator-(float3 &a)\n"
"{\n"
"    return make_float3(-a.x, -a.y, -a.z);\n"
"}\n"
"inline __host__ __device__ int3 operator-(int3 &a)\n"
"{\n"
"    return make_int3(-a.x, -a.y, -a.z);\n"
"}\n"
"inline __host__ __device__ float4 operator-(float4 &a)\n"
"{\n"
"    return make_float4(-a.x, -a.y, -a.z, -a.w);\n"
"}\n"
"inline __host__ __device__ int4 operator-(int4 &a)\n"
"{\n"
"    return make_int4(-a.x, -a.y, -a.z, -a.w);\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// addition\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline __host__ __device__ float2 operator+(float2 a, float2 b)\n"
"{\n"
"    return make_float2(a.x + b.x, a.y + b.y);\n"
"}\n"
"inline __host__ __device__ void operator+=(float2 &a, float2 b)\n"
"{\n"
"    a.x += b.x;\n"
"    a.y += b.y;\n"
"}\n"
"inline __host__ __device__ float2 operator+(float2 a, float b)\n"
"{\n"
"    return make_float2(a.x + b, a.y + b);\n"
"}\n"
"inline __host__ __device__ float2 operator+(float b, float2 a)\n"
"{\n"
"    return make_float2(a.x + b, a.y + b);\n"
"}\n"
"inline __host__ __device__ void operator+=(float2 &a, float b)\n"
"{\n"
"    a.x += b;\n"
"    a.y += b;\n"
"}\n"
"\n"
"inline __host__ __device__ int2 operator+(int2 a, int2 b)\n"
"{\n"
"    return make_int2(a.x + b.x, a.y + b.y);\n"
"}\n"
"inline __host__ __device__ void operator+=(int2 &a, int2 b)\n"
"{\n"
"    a.x += b.x;\n"
"    a.y += b.y;\n"
"}\n"
"inline __host__ __device__ int2 operator+(int2 a, int b)\n"
"{\n"
"    return make_int2(a.x + b, a.y + b);\n"
"}\n"
"inline __host__ __device__ int2 operator+(int b, int2 a)\n"
"{\n"
"    return make_int2(a.x + b, a.y + b);\n"
"}\n"
"inline __host__ __device__ void operator+=(int2 &a, int b)\n"
"{\n"
"    a.x += b;\n"
"    a.y += b;\n"
"}\n"
"\n"
"inline __host__ __device__ uint2 operator+(uint2 a, uint2 b)\n"
"{\n"
"    return make_uint2(a.x + b.x, a.y + b.y);\n"
"}\n"
"inline __host__ __device__ void operator+=(uint2 &a, uint2 b)\n"
"{\n"
"    a.x += b.x;\n"
"    a.y += b.y;\n"
"}\n"
"inline __host__ __device__ uint2 operator+(uint2 a, uint b)\n"
"{\n"
"    return make_uint2(a.x + b, a.y + b);\n"
"}\n"
"inline __host__ __device__ uint2 operator+(uint b, uint2 a)\n"
"{\n"
"    return make_uint2(a.x + b, a.y + b);\n"
"}\n"
"inline __host__ __device__ void operator+=(uint2 &a, uint b)\n"
"{\n"
"    a.x += b;\n"
"    a.y += b;\n"
"}\n"
"\n"
"\n"
"inline __host__ __device__ float3 operator+(float3 a, float3 b)\n"
"{\n"
"    return make_float3(a.x + b.x, a.y + b.y, a.z + b.z);\n"
"}\n"
"inline __host__ __device__ void operator+=(float3 &a, float3 b)\n"
"{\n"
"    a.x += b.x;\n"
"    a.y += b.y;\n"
"    a.z += b.z;\n"
"}\n"
"inline __host__ __device__ float3 operator+(float3 a, float b)\n"
"{\n"
"    return make_float3(a.x + b, a.y + b, a.z + b);\n"
"}\n"
"inline __host__ __device__ void operator+=(float3 &a, float b)\n"
"{\n"
"    a.x += b;\n"
"    a.y += b;\n"
"    a.z += b;\n"
"}\n"
"\n"
"inline __host__ __device__ int3 operator+(int3 a, int3 b)\n"
"{\n"
"    return make_int3(a.x + b.x, a.y + b.y, a.z + b.z);\n"
"}\n"
"inline __host__ __device__ void operator+=(int3 &a, int3 b)\n"
"{\n"
"    a.x += b.x;\n"
"    a.y += b.y;\n"
"    a.z += b.z;\n"
"}\n"
"inline __host__ __device__ int3 operator+(int3 a, int b)\n"
"{\n"
"    return make_int3(a.x + b, a.y + b, a.z + b);\n"
"}\n"
"inline __host__ __device__ void operator+=(int3 &a, int b)\n"
"{\n"
"    a.x += b;\n"
"    a.y += b;\n"
"    a.z += b;\n"
"}\n"
"\n"
"inline __host__ __device__ uint3 operator+(uint3 a, uint3 b)\n"
"{\n"
"    return make_uint3(a.x + b.x, a.y + b.y, a.z + b.z);\n"
"}\n"
"inline __host__ __device__ void operator+=(uint3 &a, uint3 b)\n"
"{\n"
"    a.x += b.x;\n"
"    a.y += b.y;\n"
"    a.z += b.z;\n"
"}\n"
"inline __host__ __device__ uint3 operator+(uint3 a, uint b)\n"
"{\n"
"    return make_uint3(a.x + b, a.y + b, a.z + b);\n"
"}\n"
"inline __host__ __device__ void operator+=(uint3 &a, uint b)\n"
"{\n"
"    a.x += b;\n"
"    a.y += b;\n"
"    a.z += b;\n"
"}\n"
"\n"
"inline __host__ __device__ int3 operator+(int b, int3 a)\n"
"{\n"
"    return make_int3(a.x + b, a.y + b, a.z + b);\n"
"}\n"
"inline __host__ __device__ uint3 operator+(uint b, uint3 a)\n"
"{\n"
"    return make_uint3(a.x + b, a.y + b, a.z + b);\n"
"}\n"
"inline __host__ __device__ float3 operator+(float b, float3 a)\n"
"{\n"
"    return make_float3(a.x + b, a.y + b, a.z + b);\n"
"}\n"
"\n"
"inline __host__ __device__ float4 operator+(float4 a, float4 b)\n"
"{\n"
"    return make_float4(a.x + b.x, a.y + b.y, a.z + b.z,  a.w + b.w);\n"
"}\n"
"inline __host__ __device__ void operator+=(float4 &a, float4 b)\n"
"{\n"
"    a.x += b.x;\n"
"    a.y += b.y;\n"
"    a.z += b.z;\n"
"    a.w += b.w;\n"
"}\n"
"inline __host__ __device__ float4 operator+(float4 a, float b)\n"
"{\n"
"    return make_float4(a.x + b, a.y + b, a.z + b, a.w + b);\n"
"}\n"
"inline __host__ __device__ float4 operator+(float b, float4 a)\n"
"{\n"
"    return make_float4(a.x + b, a.y + b, a.z + b, a.w + b);\n"
"}\n"
"inline __host__ __device__ void operator+=(float4 &a, float b)\n"
"{\n"
"    a.x += b;\n"
"    a.y += b;\n"
"    a.z += b;\n"
"    a.w += b;\n"
"}\n"
"\n"
"inline __host__ __device__ int4 operator+(int4 a, int4 b)\n"
"{\n"
"    return make_int4(a.x + b.x, a.y + b.y, a.z + b.z,  a.w + b.w);\n"
"}\n"
"inline __host__ __device__ void operator+=(int4 &a, int4 b)\n"
"{\n"
"    a.x += b.x;\n"
"    a.y += b.y;\n"
"    a.z += b.z;\n"
"    a.w += b.w;\n"
"}\n"
"inline __host__ __device__ int4 operator+(int4 a, int b)\n"
"{\n"
"    return make_int4(a.x + b, a.y + b, a.z + b,  a.w + b);\n"
"}\n"
"inline __host__ __device__ int4 operator+(int b, int4 a)\n"
"{\n"
"    return make_int4(a.x + b, a.y + b, a.z + b,  a.w + b);\n"
"}\n"
"inline __host__ __device__ void operator+=(int4 &a, int b)\n"
"{\n"
"    a.x += b;\n"
"    a.y += b;\n"
"    a.z += b;\n"
"    a.w += b;\n"
"}\n"
"\n"
"inline __host__ __device__ uint4 operator+(uint4 a, uint4 b)\n"
"{\n"
"    return make_uint4(a.x + b.x, a.y + b.y, a.z + b.z,  a.w + b.w);\n"
"}\n"
"inline __host__ __device__ void operator+=(uint4 &a, uint4 b)\n"
"{\n"
"    a.x += b.x;\n"
"    a.y += b.y;\n"
"    a.z += b.z;\n"
"    a.w += b.w;\n"
"}\n"
"inline __host__ __device__ uint4 operator+(uint4 a, uint b)\n"
"{\n"
"    return make_uint4(a.x + b, a.y + b, a.z + b,  a.w + b);\n"
"}\n"
"inline __host__ __device__ uint4 operator+(uint b, uint4 a)\n"
"{\n"
"    return make_uint4(a.x + b, a.y + b, a.z + b,  a.w + b);\n"
"}\n"
"inline __host__ __device__ void operator+=(uint4 &a, uint b)\n"
"{\n"
"    a.x += b;\n"
"    a.y += b;\n"
"    a.z += b;\n"
"    a.w += b;\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// subtract\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline __host__ __device__ float2 operator-(float2 a, float2 b)\n"
"{\n"
"    return make_float2(a.x - b.x, a.y - b.y);\n"
"}\n"
"inline __host__ __device__ void operator-=(float2 &a, float2 b)\n"
"{\n"
"    a.x -= b.x;\n"
"    a.y -= b.y;\n"
"}\n"
"inline __host__ __device__ float2 operator-(float2 a, float b)\n"
"{\n"
"    return make_float2(a.x - b, a.y - b);\n"
"}\n"
"inline __host__ __device__ float2 operator-(float b, float2 a)\n"
"{\n"
"    return make_float2(b - a.x, b - a.y);\n"
"}\n"
"inline __host__ __device__ void operator-=(float2 &a, float b)\n"
"{\n"
"    a.x -= b;\n"
"    a.y -= b;\n"
"}\n"
"\n"
"inline __host__ __device__ int2 operator-(int2 a, int2 b)\n"
"{\n"
"    return make_int2(a.x - b.x, a.y - b.y);\n"
"}\n"
"inline __host__ __device__ void operator-=(int2 &a, int2 b)\n"
"{\n"
"    a.x -= b.x;\n"
"    a.y -= b.y;\n"
"}\n"
"inline __host__ __device__ int2 operator-(int2 a, int b)\n"
"{\n"
"    return make_int2(a.x - b, a.y - b);\n"
"}\n"
"inline __host__ __device__ int2 operator-(int b, int2 a)\n"
"{\n"
"    return make_int2(b - a.x, b - a.y);\n"
"}\n"
"inline __host__ __device__ void operator-=(int2 &a, int b)\n"
"{\n"
"    a.x -= b;\n"
"    a.y -= b;\n"
"}\n"
"\n"
"inline __host__ __device__ uint2 operator-(uint2 a, uint2 b)\n"
"{\n"
"    return make_uint2(a.x - b.x, a.y - b.y);\n"
"}\n"
"inline __host__ __device__ void operator-=(uint2 &a, uint2 b)\n"
"{\n"
"    a.x -= b.x;\n"
"    a.y -= b.y;\n"
"}\n"
"inline __host__ __device__ uint2 operator-(uint2 a, uint b)\n"
"{\n"
"    return make_uint2(a.x - b, a.y - b);\n"
"}\n"
"inline __host__ __device__ uint2 operator-(uint b, uint2 a)\n"
"{\n"
"    return make_uint2(b - a.x, b - a.y);\n"
"}\n"
"inline __host__ __device__ void operator-=(uint2 &a, uint b)\n"
"{\n"
"    a.x -= b;\n"
"    a.y -= b;\n"
"}\n"
"\n"
"inline __host__ __device__ float3 operator-(float3 a, float3 b)\n"
"{\n"
"    return make_float3(a.x - b.x, a.y - b.y, a.z - b.z);\n"
"}\n"
"inline __host__ __device__ void operator-=(float3 &a, float3 b)\n"
"{\n"
"    a.x -= b.x;\n"
"    a.y -= b.y;\n"
"    a.z -= b.z;\n"
"}\n"
"inline __host__ __device__ float3 operator-(float3 a, float b)\n"
"{\n"
"    return make_float3(a.x - b, a.y - b, a.z - b);\n"
"}\n"
"inline __host__ __device__ float3 operator-(float b, float3 a)\n"
"{\n"
"    return make_float3(b - a.x, b - a.y, b - a.z);\n"
"}\n"
"inline __host__ __device__ void operator-=(float3 &a, float b)\n"
"{\n"
"    a.x -= b;\n"
"    a.y -= b;\n"
"    a.z -= b;\n"
"}\n"
"\n"
"inline __host__ __device__ int3 operator-(int3 a, int3 b)\n"
"{\n"
"    return make_int3(a.x - b.x, a.y - b.y, a.z - b.z);\n"
"}\n"
"inline __host__ __device__ void operator-=(int3 &a, int3 b)\n"
"{\n"
"    a.x -= b.x;\n"
"    a.y -= b.y;\n"
"    a.z -= b.z;\n"
"}\n"
"inline __host__ __device__ int3 operator-(int3 a, int b)\n"
"{\n"
"    return make_int3(a.x - b, a.y - b, a.z - b);\n"
"}\n"
"inline __host__ __device__ int3 operator-(int b, int3 a)\n"
"{\n"
"    return make_int3(b - a.x, b - a.y, b - a.z);\n"
"}\n"
"inline __host__ __device__ void operator-=(int3 &a, int b)\n"
"{\n"
"    a.x -= b;\n"
"    a.y -= b;\n"
"    a.z -= b;\n"
"}\n"
"\n"
"inline __host__ __device__ uint3 operator-(uint3 a, uint3 b)\n"
"{\n"
"    return make_uint3(a.x - b.x, a.y - b.y, a.z - b.z);\n"
"}\n"
"inline __host__ __device__ void operator-=(uint3 &a, uint3 b)\n"
"{\n"
"    a.x -= b.x;\n"
"    a.y -= b.y;\n"
"    a.z -= b.z;\n"
"}\n"
"inline __host__ __device__ uint3 operator-(uint3 a, uint b)\n"
"{\n"
"    return make_uint3(a.x - b, a.y - b, a.z - b);\n"
"}\n"
"inline __host__ __device__ uint3 operator-(uint b, uint3 a)\n"
"{\n"
"    return make_uint3(b - a.x, b - a.y, b - a.z);\n"
"}\n"
"inline __host__ __device__ void operator-=(uint3 &a, uint b)\n"
"{\n"
"    a.x -= b;\n"
"    a.y -= b;\n"
"    a.z -= b;\n"
"}\n"
"\n"
"inline __host__ __device__ float4 operator-(float4 a, float4 b)\n"
"{\n"
"    return make_float4(a.x - b.x, a.y - b.y, a.z - b.z,  a.w - b.w);\n"
"}\n"
"inline __host__ __device__ void operator-=(float4 &a, float4 b)\n"
"{\n"
"    a.x -= b.x;\n"
"    a.y -= b.y;\n"
"    a.z -= b.z;\n"
"    a.w -= b.w;\n"
"}\n"
"inline __host__ __device__ float4 operator-(float4 a, float b)\n"
"{\n"
"    return make_float4(a.x - b, a.y - b, a.z - b,  a.w - b);\n"
"}\n"
"inline __host__ __device__ void operator-=(float4 &a, float b)\n"
"{\n"
"    a.x -= b;\n"
"    a.y -= b;\n"
"    a.z -= b;\n"
"    a.w -= b;\n"
"}\n"
"\n"
"inline __host__ __device__ int4 operator-(int4 a, int4 b)\n"
"{\n"
"    return make_int4(a.x - b.x, a.y - b.y, a.z - b.z,  a.w - b.w);\n"
"}\n"
"inline __host__ __device__ void operator-=(int4 &a, int4 b)\n"
"{\n"
"    a.x -= b.x;\n"
"    a.y -= b.y;\n"
"    a.z -= b.z;\n"
"    a.w -= b.w;\n"
"}\n"
"inline __host__ __device__ int4 operator-(int4 a, int b)\n"
"{\n"
"    return make_int4(a.x - b, a.y - b, a.z - b,  a.w - b);\n"
"}\n"
"inline __host__ __device__ int4 operator-(int b, int4 a)\n"
"{\n"
"    return make_int4(b - a.x, b - a.y, b - a.z, b - a.w);\n"
"}\n"
"inline __host__ __device__ void operator-=(int4 &a, int b)\n"
"{\n"
"    a.x -= b;\n"
"    a.y -= b;\n"
"    a.z -= b;\n"
"    a.w -= b;\n"
"}\n"
"\n"
"inline __host__ __device__ uint4 operator-(uint4 a, uint4 b)\n"
"{\n"
"    return make_uint4(a.x - b.x, a.y - b.y, a.z - b.z,  a.w - b.w);\n"
"}\n"
"inline __host__ __device__ void operator-=(uint4 &a, uint4 b)\n"
"{\n"
"    a.x -= b.x;\n"
"    a.y -= b.y;\n"
"    a.z -= b.z;\n"
"    a.w -= b.w;\n"
"}\n"
"inline __host__ __device__ uint4 operator-(uint4 a, uint b)\n"
"{\n"
"    return make_uint4(a.x - b, a.y - b, a.z - b,  a.w - b);\n"
"}\n"
"inline __host__ __device__ uint4 operator-(uint b, uint4 a)\n"
"{\n"
"    return make_uint4(b - a.x, b - a.y, b - a.z, b - a.w);\n"
"}\n"
"inline __host__ __device__ void operator-=(uint4 &a, uint b)\n"
"{\n"
"    a.x -= b;\n"
"    a.y -= b;\n"
"    a.z -= b;\n"
"    a.w -= b;\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// multiply\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline __host__ __device__ float2 operator*(float2 a, float2 b)\n"
"{\n"
"    return make_float2(a.x * b.x, a.y * b.y);\n"
"}\n"
"inline __host__ __device__ void operator*=(float2 &a, float2 b)\n"
"{\n"
"    a.x *= b.x;\n"
"    a.y *= b.y;\n"
"}\n"
"inline __host__ __device__ float2 operator*(float2 a, float b)\n"
"{\n"
"    return make_float2(a.x * b, a.y * b);\n"
"}\n"
"inline __host__ __device__ float2 operator*(float b, float2 a)\n"
"{\n"
"    return make_float2(b * a.x, b * a.y);\n"
"}\n"
"inline __host__ __device__ void operator*=(float2 &a, float b)\n"
"{\n"
"    a.x *= b;\n"
"    a.y *= b;\n"
"}\n"
"\n"
"inline __host__ __device__ int2 operator*(int2 a, int2 b)\n"
"{\n"
"    return make_int2(a.x * b.x, a.y * b.y);\n"
"}\n"
"inline __host__ __device__ void operator*=(int2 &a, int2 b)\n"
"{\n"
"    a.x *= b.x;\n"
"    a.y *= b.y;\n"
"}\n"
"inline __host__ __device__ int2 operator*(int2 a, int b)\n"
"{\n"
"    return make_int2(a.x * b, a.y * b);\n"
"}\n"
"inline __host__ __device__ int2 operator*(int b, int2 a)\n"
"{\n"
"    return make_int2(b * a.x, b * a.y);\n"
"}\n"
"inline __host__ __device__ void operator*=(int2 &a, int b)\n"
"{\n"
"    a.x *= b;\n"
"    a.y *= b;\n"
"}\n"
"\n"
"inline __host__ __device__ uint2 operator*(uint2 a, uint2 b)\n"
"{\n"
"    return make_uint2(a.x * b.x, a.y * b.y);\n"
"}\n"
"inline __host__ __device__ void operator*=(uint2 &a, uint2 b)\n"
"{\n"
"    a.x *= b.x;\n"
"    a.y *= b.y;\n"
"}\n"
"inline __host__ __device__ uint2 operator*(uint2 a, uint b)\n"
"{\n"
"    return make_uint2(a.x * b, a.y * b);\n"
"}\n"
"inline __host__ __device__ uint2 operator*(uint b, uint2 a)\n"
"{\n"
"    return make_uint2(b * a.x, b * a.y);\n"
"}\n"
"inline __host__ __device__ void operator*=(uint2 &a, uint b)\n"
"{\n"
"    a.x *= b;\n"
"    a.y *= b;\n"
"}\n"
"\n"
"inline __host__ __device__ float3 operator*(float3 a, float3 b)\n"
"{\n"
"    return make_float3(a.x * b.x, a.y * b.y, a.z * b.z);\n"
"}\n"
"inline __host__ __device__ void operator*=(float3 &a, float3 b)\n"
"{\n"
"    a.x *= b.x;\n"
"    a.y *= b.y;\n"
"    a.z *= b.z;\n"
"}\n"
"inline __host__ __device__ float3 operator*(float3 a, float b)\n"
"{\n"
"    return make_float3(a.x * b, a.y * b, a.z * b);\n"
"}\n"
"inline __host__ __device__ float3 operator*(float b, float3 a)\n"
"{\n"
"    return make_float3(b * a.x, b * a.y, b * a.z);\n"
"}\n"
"inline __host__ __device__ void operator*=(float3 &a, float b)\n"
"{\n"
"    a.x *= b;\n"
"    a.y *= b;\n"
"    a.z *= b;\n"
"}\n"
"\n"
"inline __host__ __device__ int3 operator*(int3 a, int3 b)\n"
"{\n"
"    return make_int3(a.x * b.x, a.y * b.y, a.z * b.z);\n"
"}\n"
"inline __host__ __device__ void operator*=(int3 &a, int3 b)\n"
"{\n"
"    a.x *= b.x;\n"
"    a.y *= b.y;\n"
"    a.z *= b.z;\n"
"}\n"
"inline __host__ __device__ int3 operator*(int3 a, int b)\n"
"{\n"
"    return make_int3(a.x * b, a.y * b, a.z * b);\n"
"}\n"
"inline __host__ __device__ int3 operator*(int b, int3 a)\n"
"{\n"
"    return make_int3(b * a.x, b * a.y, b * a.z);\n"
"}\n"
"inline __host__ __device__ void operator*=(int3 &a, int b)\n"
"{\n"
"    a.x *= b;\n"
"    a.y *= b;\n"
"    a.z *= b;\n"
"}\n"
"\n"
"inline __host__ __device__ uint3 operator*(uint3 a, uint3 b)\n"
"{\n"
"    return make_uint3(a.x * b.x, a.y * b.y, a.z * b.z);\n"
"}\n"
"inline __host__ __device__ void operator*=(uint3 &a, uint3 b)\n"
"{\n"
"    a.x *= b.x;\n"
"    a.y *= b.y;\n"
"    a.z *= b.z;\n"
"}\n"
"inline __host__ __device__ uint3 operator*(uint3 a, uint b)\n"
"{\n"
"    return make_uint3(a.x * b, a.y * b, a.z * b);\n"
"}\n"
"inline __host__ __device__ uint3 operator*(uint b, uint3 a)\n"
"{\n"
"    return make_uint3(b * a.x, b * a.y, b * a.z);\n"
"}\n"
"inline __host__ __device__ void operator*=(uint3 &a, uint b)\n"
"{\n"
"    a.x *= b;\n"
"    a.y *= b;\n"
"    a.z *= b;\n"
"}\n"
"\n"
"inline __host__ __device__ float4 operator*(float4 a, float4 b)\n"
"{\n"
"    return make_float4(a.x * b.x, a.y * b.y, a.z * b.z,  a.w * b.w);\n"
"}\n"
"inline __host__ __device__ void operator*=(float4 &a, float4 b)\n"
"{\n"
"    a.x *= b.x;\n"
"    a.y *= b.y;\n"
"    a.z *= b.z;\n"
"    a.w *= b.w;\n"
"}\n"
"inline __host__ __device__ float4 operator*(float4 a, float b)\n"
"{\n"
"    return make_float4(a.x * b, a.y * b, a.z * b,  a.w * b);\n"
"}\n"
"inline __host__ __device__ float4 operator*(float b, float4 a)\n"
"{\n"
"    return make_float4(b * a.x, b * a.y, b * a.z, b * a.w);\n"
"}\n"
"inline __host__ __device__ void operator*=(float4 &a, float b)\n"
"{\n"
"    a.x *= b;\n"
"    a.y *= b;\n"
"    a.z *= b;\n"
"    a.w *= b;\n"
"}\n"
"\n"
"inline __host__ __device__ int4 operator*(int4 a, int4 b)\n"
"{\n"
"    return make_int4(a.x * b.x, a.y * b.y, a.z * b.z,  a.w * b.w);\n"
"}\n"
"inline __host__ __device__ void operator*=(int4 &a, int4 b)\n"
"{\n"
"    a.x *= b.x;\n"
"    a.y *= b.y;\n"
"    a.z *= b.z;\n"
"    a.w *= b.w;\n"
"}\n"
"inline __host__ __device__ int4 operator*(int4 a, int b)\n"
"{\n"
"    return make_int4(a.x * b, a.y * b, a.z * b,  a.w * b);\n"
"}\n"
"inline __host__ __device__ int4 operator*(int b, int4 a)\n"
"{\n"
"    return make_int4(b * a.x, b * a.y, b * a.z, b * a.w);\n"
"}\n"
"inline __host__ __device__ void operator*=(int4 &a, int b)\n"
"{\n"
"    a.x *= b;\n"
"    a.y *= b;\n"
"    a.z *= b;\n"
"    a.w *= b;\n"
"}\n"
"\n"
"inline __host__ __device__ uint4 operator*(uint4 a, uint4 b)\n"
"{\n"
"    return make_uint4(a.x * b.x, a.y * b.y, a.z * b.z,  a.w * b.w);\n"
"}\n"
"inline __host__ __device__ void operator*=(uint4 &a, uint4 b)\n"
"{\n"
"    a.x *= b.x;\n"
"    a.y *= b.y;\n"
"    a.z *= b.z;\n"
"    a.w *= b.w;\n"
"}\n"
"inline __host__ __device__ uint4 operator*(uint4 a, uint b)\n"
"{\n"
"    return make_uint4(a.x * b, a.y * b, a.z * b,  a.w * b);\n"
"}\n"
"inline __host__ __device__ uint4 operator*(uint b, uint4 a)\n"
"{\n"
"    return make_uint4(b * a.x, b * a.y, b * a.z, b * a.w);\n"
"}\n"
"inline __host__ __device__ void operator*=(uint4 &a, uint b)\n"
"{\n"
"    a.x *= b;\n"
"    a.y *= b;\n"
"    a.z *= b;\n"
"    a.w *= b;\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// divide\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline __host__ __device__ float2 operator/(float2 a, float2 b)\n"
"{\n"
"    return make_float2(a.x / b.x, a.y / b.y);\n"
"}\n"
"inline __host__ __device__ void operator/=(float2 &a, float2 b)\n"
"{\n"
"    a.x /= b.x;\n"
"    a.y /= b.y;\n"
"}\n"
"inline __host__ __device__ float2 operator/(float2 a, float b)\n"
"{\n"
"    return make_float2(a.x / b, a.y / b);\n"
"}\n"
"inline __host__ __device__ void operator/=(float2 &a, float b)\n"
"{\n"
"    a.x /= b;\n"
"    a.y /= b;\n"
"}\n"
"inline __host__ __device__ float2 operator/(float b, float2 a)\n"
"{\n"
"    return make_float2(b / a.x, b / a.y);\n"
"}\n"
"\n"
"inline __host__ __device__ float3 operator/(float3 a, float3 b)\n"
"{\n"
"    return make_float3(a.x / b.x, a.y / b.y, a.z / b.z);\n"
"}\n"
"inline __host__ __device__ void operator/=(float3 &a, float3 b)\n"
"{\n"
"    a.x /= b.x;\n"
"    a.y /= b.y;\n"
"    a.z /= b.z;\n"
"}\n"
"inline __host__ __device__ float3 operator/(float3 a, float b)\n"
"{\n"
"    return make_float3(a.x / b, a.y / b, a.z / b);\n"
"}\n"
"inline __host__ __device__ void operator/=(float3 &a, float b)\n"
"{\n"
"    a.x /= b;\n"
"    a.y /= b;\n"
"    a.z /= b;\n"
"}\n"
"inline __host__ __device__ float3 operator/(float b, float3 a)\n"
"{\n"
"    return make_float3(b / a.x, b / a.y, b / a.z);\n"
"}\n"
"\n"
"inline __host__ __device__ float4 operator/(float4 a, float4 b)\n"
"{\n"
"    return make_float4(a.x / b.x, a.y / b.y, a.z / b.z,  a.w / b.w);\n"
"}\n"
"inline __host__ __device__ void operator/=(float4 &a, float4 b)\n"
"{\n"
"    a.x /= b.x;\n"
"    a.y /= b.y;\n"
"    a.z /= b.z;\n"
"    a.w /= b.w;\n"
"}\n"
"inline __host__ __device__ float4 operator/(float4 a, float b)\n"
"{\n"
"    return make_float4(a.x / b, a.y / b, a.z / b,  a.w / b);\n"
"}\n"
"inline __host__ __device__ void operator/=(float4 &a, float b)\n"
"{\n"
"    a.x /= b;\n"
"    a.y /= b;\n"
"    a.z /= b;\n"
"    a.w /= b;\n"
"}\n"
"inline __host__ __device__ float4 operator/(float b, float4 a)\n"
"{\n"
"    return make_float4(b / a.x, b / a.y, b / a.z, b / a.w);\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// min\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline  __host__ __device__ float2 fminf(float2 a, float2 b)\n"
"{\n"
"    return make_float2(fminf(a.x,b.x), fminf(a.y,b.y));\n"
"}\n"
"inline __host__ __device__ float3 fminf(float3 a, float3 b)\n"
"{\n"
"    return make_float3(fminf(a.x,b.x), fminf(a.y,b.y), fminf(a.z,b.z));\n"
"}\n"
"inline  __host__ __device__ float4 fminf(float4 a, float4 b)\n"
"{\n"
"    return make_float4(fminf(a.x,b.x), fminf(a.y,b.y), fminf(a.z,b.z), fminf(a.w,b.w));\n"
"}\n"
"\n"
"inline __host__ __device__ int2 min(int2 a, int2 b)\n"
"{\n"
"    return make_int2(min(a.x,b.x), min(a.y,b.y));\n"
"}\n"
"inline __host__ __device__ int3 min(int3 a, int3 b)\n"
"{\n"
"    return make_int3(min(a.x,b.x), min(a.y,b.y), min(a.z,b.z));\n"
"}\n"
"inline __host__ __device__ int4 min(int4 a, int4 b)\n"
"{\n"
"    return make_int4(min(a.x,b.x), min(a.y,b.y), min(a.z,b.z), min(a.w,b.w));\n"
"}\n"
"\n"
"inline __host__ __device__ uint2 min(uint2 a, uint2 b)\n"
"{\n"
"    return make_uint2(min(a.x,b.x), min(a.y,b.y));\n"
"}\n"
"inline __host__ __device__ uint3 min(uint3 a, uint3 b)\n"
"{\n"
"    return make_uint3(min(a.x,b.x), min(a.y,b.y), min(a.z,b.z));\n"
"}\n"
"inline __host__ __device__ uint4 min(uint4 a, uint4 b)\n"
"{\n"
"    return make_uint4(min(a.x,b.x), min(a.y,b.y), min(a.z,b.z), min(a.w,b.w));\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// max\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline __host__ __device__ float2 fmaxf(float2 a, float2 b)\n"
"{\n"
"    return make_float2(fmaxf(a.x,b.x), fmaxf(a.y,b.y));\n"
"}\n"
"inline __host__ __device__ float3 fmaxf(float3 a, float3 b)\n"
"{\n"
"    return make_float3(fmaxf(a.x,b.x), fmaxf(a.y,b.y), fmaxf(a.z,b.z));\n"
"}\n"
"inline __host__ __device__ float4 fmaxf(float4 a, float4 b)\n"
"{\n"
"    return make_float4(fmaxf(a.x,b.x), fmaxf(a.y,b.y), fmaxf(a.z,b.z), fmaxf(a.w,b.w));\n"
"}\n"
"\n"
"inline __host__ __device__ int2 max(int2 a, int2 b)\n"
"{\n"
"    return make_int2(max(a.x,b.x), max(a.y,b.y));\n"
"}\n"
"inline __host__ __device__ int3 max(int3 a, int3 b)\n"
"{\n"
"    return make_int3(max(a.x,b.x), max(a.y,b.y), max(a.z,b.z));\n"
"}\n"
"inline __host__ __device__ int4 max(int4 a, int4 b)\n"
"{\n"
"    return make_int4(max(a.x,b.x), max(a.y,b.y), max(a.z,b.z), max(a.w,b.w));\n"
"}\n"
"\n"
"inline __host__ __device__ uint2 max(uint2 a, uint2 b)\n"
"{\n"
"    return make_uint2(max(a.x,b.x), max(a.y,b.y));\n"
"}\n"
"inline __host__ __device__ uint3 max(uint3 a, uint3 b)\n"
"{\n"
"    return make_uint3(max(a.x,b.x), max(a.y,b.y), max(a.z,b.z));\n"
"}\n"
"inline __host__ __device__ uint4 max(uint4 a, uint4 b)\n"
"{\n"
"    return make_uint4(max(a.x,b.x), max(a.y,b.y), max(a.z,b.z), max(a.w,b.w));\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// lerp\n"
"// - linear interpolation between a and b, based on value t in [0, 1] range\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline __device__ __host__ float lerp(float a, float b, float t)\n"
"{\n"
"    return a + t*(b-a);\n"
"}\n"
"inline __device__ __host__ float2 lerp(float2 a, float2 b, float t)\n"
"{\n"
"    return a + t*(b-a);\n"
"}\n"
"inline __device__ __host__ float3 lerp(float3 a, float3 b, float t)\n"
"{\n"
"    return a + t*(b-a);\n"
"}\n"
"inline __device__ __host__ float4 lerp(float4 a, float4 b, float t)\n"
"{\n"
"    return a + t*(b-a);\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// clamp\n"
"// - clamp the value v to be in the range [a, b]\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline __device__ __host__ float clamp(float f, float a, float b)\n"
"{\n"
"    return fmaxf(a, fminf(f, b));\n"
"}\n"
"inline __device__ __host__ int clamp(int f, int a, int b)\n"
"{\n"
"    return max(a, min(f, b));\n"
"}\n"
"inline __device__ __host__ uint clamp(uint f, uint a, uint b)\n"
"{\n"
"    return max(a, min(f, b));\n"
"}\n"
"\n"
"inline __device__ __host__ float2 clamp(float2 v, float a, float b)\n"
"{\n"
"    return make_float2(clamp(v.x, a, b), clamp(v.y, a, b));\n"
"}\n"
"inline __device__ __host__ float2 clamp(float2 v, float2 a, float2 b)\n"
"{\n"
"    return make_float2(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y));\n"
"}\n"
"inline __device__ __host__ float3 clamp(float3 v, float a, float b)\n"
"{\n"
"    return make_float3(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b));\n"
"}\n"
"inline __device__ __host__ float3 clamp(float3 v, float3 a, float3 b)\n"
"{\n"
"    return make_float3(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z));\n"
"}\n"
"inline __device__ __host__ float4 clamp(float4 v, float a, float b)\n"
"{\n"
"    return make_float4(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b), clamp(v.w, a, b));\n"
"}\n"
"inline __device__ __host__ float4 clamp(float4 v, float4 a, float4 b)\n"
"{\n"
"    return make_float4(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z), clamp(v.w, a.w, b.w));\n"
"}\n"
"\n"
"inline __device__ __host__ int2 clamp(int2 v, int a, int b)\n"
"{\n"
"    return make_int2(clamp(v.x, a, b), clamp(v.y, a, b));\n"
"}\n"
"inline __device__ __host__ int2 clamp(int2 v, int2 a, int2 b)\n"
"{\n"
"    return make_int2(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y));\n"
"}\n"
"inline __device__ __host__ int3 clamp(int3 v, int a, int b)\n"
"{\n"
"    return make_int3(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b));\n"
"}\n"
"inline __device__ __host__ int3 clamp(int3 v, int3 a, int3 b)\n"
"{\n"
"    return make_int3(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z));\n"
"}\n"
"inline __device__ __host__ int4 clamp(int4 v, int a, int b)\n"
"{\n"
"    return make_int4(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b), clamp(v.w, a, b));\n"
"}\n"
"inline __device__ __host__ int4 clamp(int4 v, int4 a, int4 b)\n"
"{\n"
"    return make_int4(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z), clamp(v.w, a.w, b.w));\n"
"}\n"
"\n"
"inline __device__ __host__ uint2 clamp(uint2 v, uint a, uint b)\n"
"{\n"
"    return make_uint2(clamp(v.x, a, b), clamp(v.y, a, b));\n"
"}\n"
"inline __device__ __host__ uint2 clamp(uint2 v, uint2 a, uint2 b)\n"
"{\n"
"    return make_uint2(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y));\n"
"}\n"
"inline __device__ __host__ uint3 clamp(uint3 v, uint a, uint b)\n"
"{\n"
"    return make_uint3(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b));\n"
"}\n"
"inline __device__ __host__ uint3 clamp(uint3 v, uint3 a, uint3 b)\n"
"{\n"
"    return make_uint3(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z));\n"
"}\n"
"inline __device__ __host__ uint4 clamp(uint4 v, uint a, uint b)\n"
"{\n"
"    return make_uint4(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b), clamp(v.w, a, b));\n"
"}\n"
"inline __device__ __host__ uint4 clamp(uint4 v, uint4 a, uint4 b)\n"
"{\n"
"    return make_uint4(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z), clamp(v.w, a.w, b.w));\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// dot product\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline __host__ __device__ float dot(float2 a, float2 b)\n"
"{\n"
"    return a.x * b.x + a.y * b.y;\n"
"}\n"
"inline __host__ __device__ float dot(float3 a, float3 b)\n"
"{\n"
"    return a.x * b.x + a.y * b.y + a.z * b.z;\n"
"}\n"
"inline __host__ __device__ float dot(float4 a, float4 b)\n"
"{\n"
"    return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;\n"
"}\n"
"\n"
"inline __host__ __device__ int dot(int2 a, int2 b)\n"
"{\n"
"    return a.x * b.x + a.y * b.y;\n"
"}\n"
"inline __host__ __device__ int dot(int3 a, int3 b)\n"
"{\n"
"    return a.x * b.x + a.y * b.y + a.z * b.z;\n"
"}\n"
"inline __host__ __device__ int dot(int4 a, int4 b)\n"
"{\n"
"    return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;\n"
"}\n"
"\n"
"inline __host__ __device__ uint dot(uint2 a, uint2 b)\n"
"{\n"
"    return a.x * b.x + a.y * b.y;\n"
"}\n"
"inline __host__ __device__ uint dot(uint3 a, uint3 b)\n"
"{\n"
"    return a.x * b.x + a.y * b.y + a.z * b.z;\n"
"}\n"
"inline __host__ __device__ uint dot(uint4 a, uint4 b)\n"
"{\n"
"    return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// length\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline __host__ __device__ float length(float2 v)\n"
"{\n"
"    return sqrtf(dot(v, v));\n"
"}\n"
"inline __host__ __device__ float length(float3 v)\n"
"{\n"
"    return sqrtf(dot(v, v));\n"
"}\n"
"inline __host__ __device__ float length(float4 v)\n"
"{\n"
"    return sqrtf(dot(v, v));\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// normalize\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline __host__ __device__ float2 normalize(float2 v)\n"
"{\n"
"    float invLen = rsqrtf(dot(v, v));\n"
"    return v * invLen;\n"
"}\n"
"inline __host__ __device__ float3 normalize(float3 v)\n"
"{\n"
"    float invLen = rsqrtf(dot(v, v));\n"
"    return v * invLen;\n"
"}\n"
"inline __host__ __device__ float4 normalize(float4 v)\n"
"{\n"
"    float invLen = rsqrtf(dot(v, v));\n"
"    return v * invLen;\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// floor\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline __host__ __device__ float2 floorf(float2 v)\n"
"{\n"
"    return make_float2(floorf(v.x), floorf(v.y));\n"
"}\n"
"inline __host__ __device__ float3 floorf(float3 v)\n"
"{\n"
"    return make_float3(floorf(v.x), floorf(v.y), floorf(v.z));\n"
"}\n"
"inline __host__ __device__ float4 floorf(float4 v)\n"
"{\n"
"    return make_float4(floorf(v.x), floorf(v.y), floorf(v.z), floorf(v.w));\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// frac - returns the fractional portion of a scalar or each vector component\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline __host__ __device__ float fracf(float v)\n"
"{\n"
"    return v - floorf(v);\n"
"}\n"
"inline __host__ __device__ float2 fracf(float2 v)\n"
"{\n"
"    return make_float2(fracf(v.x), fracf(v.y));\n"
"}\n"
"inline __host__ __device__ float3 fracf(float3 v)\n"
"{\n"
"    return make_float3(fracf(v.x), fracf(v.y), fracf(v.z));\n"
"}\n"
"inline __host__ __device__ float4 fracf(float4 v)\n"
"{\n"
"    return make_float4(fracf(v.x), fracf(v.y), fracf(v.z), fracf(v.w));\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// fmod\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline __host__ __device__ float2 fmodf(float2 a, float2 b)\n"
"{\n"
"    return make_float2(fmodf(a.x, b.x), fmodf(a.y, b.y));\n"
"}\n"
"inline __host__ __device__ float3 fmodf(float3 a, float3 b)\n"
"{\n"
"    return make_float3(fmodf(a.x, b.x), fmodf(a.y, b.y), fmodf(a.z, b.z));\n"
"}\n"
"inline __host__ __device__ float4 fmodf(float4 a, float4 b)\n"
"{\n"
"    return make_float4(fmodf(a.x, b.x), fmodf(a.y, b.y), fmodf(a.z, b.z), fmodf(a.w, b.w));\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// absolute value\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline __host__ __device__ float2 fabs(float2 v)\n"
"{\n"
"    return make_float2(fabs(v.x), fabs(v.y));\n"
"}\n"
"inline __host__ __device__ float3 fabs(float3 v)\n"
"{\n"
"    return make_float3(fabs(v.x), fabs(v.y), fabs(v.z));\n"
"}\n"
"inline __host__ __device__ float4 fabs(float4 v)\n"
"{\n"
"    return make_float4(fabs(v.x), fabs(v.y), fabs(v.z), fabs(v.w));\n"
"}\n"
"\n"
"inline __host__ __device__ int2 abs(int2 v)\n"
"{\n"
"    return make_int2(abs(v.x), abs(v.y));\n"
"}\n"
"inline __host__ __device__ int3 abs(int3 v)\n"
"{\n"
"    return make_int3(abs(v.x), abs(v.y), abs(v.z));\n"
"}\n"
"inline __host__ __device__ int4 abs(int4 v)\n"
"{\n"
"    return make_int4(abs(v.x), abs(v.y), abs(v.z), abs(v.w));\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// reflect\n"
"// - returns reflection of incident ray I around surface normal N\n"
"// - N should be normalized, reflected vector's length is equal to length of I\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline __host__ __device__ float3 reflect(float3 i, float3 n)\n"
"{\n"
"    return i - 2.0f * n * dot(n,i);\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// cross product\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline __host__ __device__ float3 cross(float3 a, float3 b)\n"
"{\n"
"    return make_float3(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"// smoothstep\n"
"// - returns 0 if x < a\n"
"// - returns 1 if x > b\n"
"// - otherwise returns smooth interpolation between 0 and 1 based on x\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"inline __device__ __host__ float smoothstep(float a, float b, float x)\n"
"{\n"
"    float y = clamp((x - a) / (b - a), 0.0f, 1.0f);\n"
"    return (y*y*(3.0f - (2.0f*y)));\n"
"}\n"
"inline __device__ __host__ float2 smoothstep(float2 a, float2 b, float2 x)\n"
"{\n"
"    float2 y = clamp((x - a) / (b - a), 0.0f, 1.0f);\n"
"    return (y*y*(make_float2(3.0f) - (make_float2(2.0f)*y)));\n"
"}\n"
"inline __device__ __host__ float3 smoothstep(float3 a, float3 b, float3 x)\n"
"{\n"
"    float3 y = clamp((x - a) / (b - a), 0.0f, 1.0f);\n"
"    return (y*y*(make_float3(3.0f) - (make_float3(2.0f)*y)));\n"
"}\n"
"inline __device__ __host__ float4 smoothstep(float4 a, float4 b, float4 x)\n"
"{\n"
"    float4 y = clamp((x - a) / (b - a), 0.0f, 1.0f);\n"
"    return (y*y*(make_float4(3.0f) - (make_float4(2.0f)*y)));\n"
"}\n"
"\n"
"#endif");
} // end of function
} // end namespace gpuip

